package com.slz3.business.module.systemManagement.sysUser.steward;

import com.google.gson.Gson;
import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.RedisClient;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.systemManagement.sysUser.domain.SysUser;
import com.slz3.business.module.systemManagement.sysUser.domain.SysUserCondition;
import com.slz3.business.module.systemManagement.sysUser.domain.SysUserFactory;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.UUID;

public class SysUserResExecutor {

    static Mono<ServerResponse> add(ServerRequest req, SysUser sysUser) {
        SysUserConverter.setSaveUpdateUser(req, sysUser, true);
        SysUser sysUserDBLatest = Services.sysUserTransactional.save(sysUser);
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(sysUserDBLatest).build());
    }

    /**
     * known: sysUserList is not null
     */
    public static Mono<ServerResponse> delete(ServerRequest req) {
        List<SysUser> sysUserList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(sysUserList)) {
            List<SysUser> sysUserListNew = Services.sysUser.delete(sysUserList);
            if (VE.isNotEmpty(sysUserListNew) && sysUserListNew.size() == sysUserList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(sysUserListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("delete fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> modify(ServerRequest req, SysUser source) {
        SysUserConverter.setSaveUpdateUser(req, source, false);
        SysUser target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            Steward.merge(source, target);
            SysUser sysUserDBLatest = Services.sysUserTransactional.modify(target);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(sysUserDBLatest).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> list(ServerRequest req) {
        Page<SysUser> sysUserPage = Services.sysUser.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(sysUserPage).build());
    }

    static Mono<ServerResponse> info(ServerRequest req) {
        SysUser target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> choice(ServerRequest req) {
        Page<SysUser> sysUserPage = Services.sysUser.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(sysUserPage).build());
    }

    /**
     * known: sysUserList is not null
     */
    public static Mono<ServerResponse> auditSubmit(ServerRequest req) {
        List<SysUser> sysUserList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(sysUserList)) {
            sysUserList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.WAIT_AUDIT);
                SysUserFactory.initBaseFields(item);
            });
            List<SysUser> sysUserListNew = Services.sysUserTransactional.batchModify(sysUserList);
            if (VE.isNotEmpty(sysUserListNew) && sysUserListNew.size() == sysUserList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(sysUserListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditSubmit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: sysUserList is not null
     */
    public static Mono<ServerResponse> audit(ServerRequest req) {
        List<SysUser> sysUserList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(sysUserList)) {
            sysUserList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.APPROVED);
                SysUserFactory.initBaseFields(item);
            });
            List<SysUser> sysUserListNew = Services.sysUserTransactional.batchModify(sysUserList);
            if (VE.isNotEmpty(sysUserListNew) && sysUserListNew.size() == sysUserList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(sysUserListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("audit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: sysUserList is not null
     */
    public static Mono<ServerResponse> auditReject(ServerRequest req) {
        List<SysUser> sysUserList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(sysUserList)) {
            sysUserList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.REJECT);
                SysUserFactory.initBaseFields(item);
            });
            List<SysUser> sysUserListNew = Services.sysUserTransactional.batchModify(sysUserList);
            if (VE.isNotEmpty(sysUserListNew) && sysUserListNew.size() == sysUserList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(sysUserListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: sysUserList is not null
     */
    public static Mono<ServerResponse> usingEnable(ServerRequest req) {
        List<SysUser> sysUserList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(sysUserList)) {
            sysUserList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.ENABLE);
                SysUserFactory.initBaseFields(item);
            });
            List<SysUser> sysUserListNew = Services.sysUserTransactional.batchModify(sysUserList);
            if (VE.isNotEmpty(sysUserListNew) && sysUserListNew.size() == sysUserList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(sysUserListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }


    /**
     * known: sysUserList is not null
     */
    public static Mono<ServerResponse> usingDisable(ServerRequest req) {
        List<SysUser> sysUserList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(sysUserList)) {
            sysUserList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.DISABLE);
                SysUserFactory.initBaseFields(item);
            });
            List<SysUser> sysUserListNew = Services.sysUserTransactional.batchModify(sysUserList);
            if (VE.isNotEmpty(sysUserListNew) && sysUserListNew.size() == sysUserList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(sysUserListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("usingDisable fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    public static Mono<ServerResponse> login(ServerRequest req, SysUser sysUser) {
        SysUser find = Services.sysUserTransactional.login(sysUser.getSerName(), sysUser.getSerPass());
        if (find == null){
            return RR.fail(req, ResInfo.builder().code("not found").theme("登陆失败").build());
        }else {
            SysUserConverter.saveLoginUser2Redis(find);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(find).build());
        }
    }
}
